<?php
//require('parse.php');

abstract class craw_tienganhhangngay_com
{
	static protected $_url = 'http://tienganhhangngay.com';
	
	function __construct()
	{
		//self::$_url	=	'http://tienganhhangngay.com';
		//die('aaaa');
	}
	
	function get_url()
	{
		return self::$_url;
	}
	
	static function get_cats()
	{
		global $db;
		//die(self::get_url());
		$html = new simpleDom();
		$html->load_file(self::get_url());
		
		$lis	=	$html->find('.cat-item');
		$cats	=	array();
		
		foreach($lis as $li)
		{
			$name	=	$li->find('a',0)->text();
			$link	=	$li->find('a',0)->href;
			$ul	=	@$li->find('ul',0);
			//$c = array();
			if($ul)
			{
				$c_tmp = array();
				foreach($ul->find('li') as $li)
				{
					$c_name	=	$li->find('a',0)->text();
					$c_link	=	$li->find('a',0)->href;
					$c_tmp['name']	=	$c_name;
					$c_tmp['link']	=	$c_link;
					//$tmp['chil'][]	=	$c_tmp;
				}
				
			}
			
			if(!self::check_cat_exist($name))
			{
				$obj = new JObject($db,'#__craw_cats');
				$obj->name	=	$name;
				$obj->source_link	=	$link;
				$obj->parent_id	=	0;
				$obj->store();
				echo "INSERT $name is successed!<br />";
			}
			
		}
		
		self::update_total_page();
	}
	
	static function get_article()
	{
		
	}
	
	static function check_cat_exist($name = NULL)
	{
		global $db;
		$query	=	"SELECT * FROM #__craw_cats WHERE name=".$db->Quote($name);
		$obj	=	$db->setQuery($query);
		$query	=	$db->loadObject();
		
		if($query)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	static function update_total_page()
	{
		global $db;
		$query	=	"SELECT * FROM #__craw_cats";
		$query	=	$db->setQuery($query);
		
		$list	=	$db->loadObjectList();
		//die('=======');
		foreach($list as $row)
		{
			$html	=	new	simpleDom();
			$html->load_file($row->source_link);
			$as	=	@$html->find('.wp-pagenavi',0);
			//$html->clear();
			$count	=	count($as);
			if($as)
			{
				$span	=	$as->find('.pages',0)->text();
				preg_match('/.*?of.*?([\d]+).*?/', $span, $arr);
				//print_r($arr);
				if($arr)
				{
					$db->setQuery("UPDATE #__craw_cats SET total_page=".$db->Quote($arr[1])." WHERE id=".$db->Quote($row->id));
					$db->query();
					echo "<p>UPdate total page success for ".$row->name."</p>";
				}
				
				flush(); ob_flush();
			}
			//$a_last	=	$as[$count-1]->text;
			
		}
	}
	
	static function get_title_article()
	{
		global $db;
		$query	=	"SELECT * FROM #__craw_cats";
		$query	=	$db->setQuery($query);
		
		$cats	=	$db->loadObjectList();
		
		//print_r($cats);die();
		
		foreach($cats as $cat)
		{
			$total_page	=	($cat->total_page > 1 ? $cat->total_page : 1);
			//die($total_page);
			for($i=1; $i<= $total_page; $i++)
			{
				if($i==1)
				{
					$url	=	$cat->source_link;
				}
				else
				{
					$url	=	$cat->source_link.'/page/'.$i;
				}
				self::__get_title($url);
			}
			
		}
		
	}
	
	static function __get_title($url = null)
	{
		global $db;
		//die($url);
		$html	=	new	simpleDom();
		$html->load_file($url);
			
		$titles	=	$html->find('.post-meta',0);
		//echo $titles->xmltext();die($url);
		foreach($titles->find('h1 a') as $a)
		{
			$title	=	$a->text();
			//die($title);
			$link	=	$a->href;
			$title_link	=	string::ascii_link($title);
		
			if(!self::check_title_existed($title,$cat->id))
			{
				$obj = new JObject($db,'#__craw_articles');
				$obj->title	=	$title;
				$obj->title_link	=	$title_link;
				$obj->source_link	=	$link;
				$obj->catid	=	$cat->id;
				$obj->store();
				echo "<p>INSERT title $title success!</p>";
				flush(); ob_flush();
			}
				
		}
	
		//clear simple_dom
		$html->clear();
	}
	
	static function get_content_article()
	{
		global $db;
		$query	=	$db->setQuery("SELECT * FROM #__craw_articles WHERE status=0");
		
		$rows	=	$db->loadObjectList();
		
		foreach($rows as $row)
		{
			$html	=	new	simpleDom();
			$html->load_file($row->source_link);
			
			$content	=	$html->find('.post-content',0)->xmltext();
			$content	=	strip_tags($content,'<p><div><span><em><ul><li><h*><br/><br /><br><table><tr><td><th><tbody><thead><ol><bb><dd><ll><hr/><hr><hr />');
			$content	=	json_encode($content);
			//die($row->source_link);
			//echo $content;die();
			$db->setQuery("UPDATE #__craw_articles SET content=".$db->Quote($content).",status=1,time=".$db->Quote(date('Y-m-d H:i:s'))." WHERE id=".$db->Quote($row->id));
			if($db->query())
			{
				echo "<p>Update article ".$row->title." success!</p>";
				flush(); ob_flush();
			}
			else
			{
				echo $db->getErrorMsg();
			}
		}
		
		//clear simple dom
		$html->clear();
	}
	
	static function update_name_ascii_cat()
	{
		global $db;
		$query	=	$db->setQuery("SELECT * FROM #__craw_cats");
		
		$rows	=	$db->loadObjectList();
		foreach($rows as $row)
		{
			$name_ascii	=	string::ascii_link($row->name);
			//die($row->name);
			$db->setQuery("UPDATE #__craw_cats SET name_ascii=".$db->Quote($name_ascii)." WHERE id=".$db->Quote($row->id));
			if($db->query())
			{
				echo "<p>Update name_ascii $name_ascii success!</p>";
				flush(); ob_flush();
			}
		}
	}
	
	static function update_title_link()
	{
		global $db;
		$query	=	$db->setQuery("SELECT * FROM #__craw_articles");
		
		$rows	=	$db->loadObjectList();
		foreach($rows as $row)
		{
			$title_link	=	string::ascii_link($row->title);
			//die($row->name);
			$db->setQuery("UPDATE #__craw_articles SET title_link=".$db->Quote($title_link)." WHERE id=".$db->Quote($row->id));
			if($db->query())
			{
				echo "<p>Update title_link $title_link success!</p>";
				flush(); ob_flush();
			}
			}
	}
	
	static function check_title_existed($title = NULL,$catid = NULL)
	{
		global $db;
		
		$query	=	$db->setQuery("SELECT * FROM #__craw_articles WHERE title=".$db->Quote($title))." AND catid=".$db->Quote($catid);
		$row	=	$db->loadObject();
		
		if($row)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}